package com.daocao.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daocao.common.constants.HttpStatus;
import com.daocao.common.domain.entity.UmsDetailResume;
import com.daocao.common.domain.entity.UmsUserDetail;
import com.daocao.common.exception.ServiceException;
import com.daocao.common.mapper.UmsUserDetailMapper;
import com.daocao.common.service.IUmsDetailResumeService;
import com.daocao.common.service.IUmsUserDetailService;
import com.daocao.common.utils.security.DaoCaoSecurityUtil;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class UmsUserDetailServiceImpl extends ServiceImpl<UmsUserDetailMapper, UmsUserDetail> implements IUmsUserDetailService{

    @Autowired
    private UmsUserDetailMapper userDetailMapper;

    @Autowired
    private IUmsDetailResumeService detailResumeService;

    //private final static String TOPIC_NAME = "user-DBaccess-topic";

    //@Autowired
    //private KafkaTemplate<String, String> kafkaTemplate;


    @Override
    public UmsUserDetail searchById() {
        // 获取用户id
        Long userId = DaoCaoSecurityUtil.getUserId();
        UmsUserDetail umsUserDetail = userDetailMapper.selectForId(userId);
        if(ObjectUtil.isNull(umsUserDetail)) {
            throw new ServiceException(HttpStatus.FORBIDDEN,"");
        }
        return umsUserDetail;
    }

    @Override
    public int updateDetail(UmsUserDetail umsUserDetail) {
        // 获取用户id
        Long userId = DaoCaoSecurityUtil.getUserId();
        umsUserDetail.setId(userId);
        // 判断是否登录
        String creator = DaoCaoSecurityUtil.getUsername();
        umsUserDetail.setUpdater(creator);
        // 判断是否存在
        String address1 = umsUserDetail.getAddress1();
        String address2 = umsUserDetail.getAddress2();
        String phonenum = umsUserDetail.getPhonenum();
        String passcode = umsUserDetail.getPasscode();
        String companyAddress1 = umsUserDetail.getCompanyAddress1();
        String companyAddress2 = umsUserDetail.getCompanyAddress2();
        String companyPhonenum = umsUserDetail.getCompanyPhonenum();
        String companyPasscode = umsUserDetail.getCompanyPasscode();

        LambdaQueryWrapper<UmsUserDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsUserDetail::getAddress1, address1)
               .eq(UmsUserDetail::getAddress2, address2)
               .eq(UmsUserDetail::getPhonenum, phonenum)
               .eq(UmsUserDetail::getPasscode, passcode)
               .eq(UmsUserDetail::getCompanyAddress1, companyAddress1)
               .eq(UmsUserDetail::getCompanyAddress2, companyAddress2)
               .eq(UmsUserDetail::getCompanyPhonenum, companyPhonenum)
               .eq(UmsUserDetail::getCompanyPasscode, companyPasscode)
               .eq(UmsUserDetail::getId, DaoCaoSecurityUtil.getUserId());
        Long count = baseMapper.selectCount(wrapper);
        log.info(count.toString());
        if (count > 0) {
            throw new ServiceException(HttpStatus.ERROR, "変更項目を変更したら、登録してください。");
        }
        // 履历表insert处理
        UmsDetailResume detailResume = new UmsDetailResume();
        //变更前的明细表内容取得
        UmsUserDetail umsUserDetailBefore =  userDetailMapper.selectById(userId);
        detailResume.setId(userId);
        detailResume.setKanjiname(umsUserDetailBefore.getKanjiname());
        detailResume.setKananame(umsUserDetailBefore.getKananame());
        detailResume.setAddress1(umsUserDetailBefore.getAddress1());
        detailResume.setAddress2(umsUserDetailBefore.getAddress2());
        detailResume.setPhonenum(umsUserDetailBefore.getPhonenum());
        detailResume.setPasscode(umsUserDetailBefore.getPasscode());
        detailResume.setCompanyAddress1(umsUserDetailBefore.getCompanyAddress1());
        detailResume.setCompanyAddress2(umsUserDetailBefore.getCompanyAddress2());
        detailResume.setCompanyPhonenum(umsUserDetailBefore.getCompanyPhonenum());
        detailResume.setCompanyPasscode(umsUserDetailBefore.getCompanyPasscode());

        detailResumeService.insertDetailResume(detailResume);

       List<Object> recordList = new ArrayList<>();
        recordList.add(userId);
        recordList.add(creator);
        recordList.add(detailResume);
        //kafkaTemplate.send(TOPIC_NAME, 0, "INSERT", recordList.toString());
        //List<Object> recordList = new ArrayList<>();
        //recordList.add(DaoCaoSecurityUtil.getLoginUser());
        //recordList.add(DaoCaoSecurityUtil.getUserId());
        //recordList.add(DaoCaoSecurityUtil.getUsername());
        //recordList.add(umsUserDetail);
        //kafkaTemplate.send(TOPIC_NAME,0,"UPDATE",recordList.toString());
        //System.out.println("update"+recordList);
        return baseMapper.updateById(umsUserDetail);
    }
}
